package com.homegoods.orderinfodetail.item;

import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;

import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.orderinfodetail.OrderInfoApiUtils;
import com.homegoods.orderinfodetail.R;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.orderinfodetail.bean.AddressListModel;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.dialog.ECAlertDialog;

/**
 * @author QiQi
 * @ClassName: AddressManagerItem
 * @date 2018/9/29
 * @description
 */
public class AddressManagerItem extends AddressItem {
    private static final String TAG = "AddressManagerItemTag";
    private MultiTypeAdapter adapter;

    public AddressManagerItem(AddressListModel data, MultiTypeAdapter adapter) {
        super(data);
        this.adapter = adapter;
        setOnClickListener(v -> {
            if (adapter.getOnItemClickListener() != null) {
                adapter.getOnItemClickListener().onItemClicked(v, this);
            }
        });
    }


    @Override
    public int getLayout() {
        return R.layout.info_item_address_manager;
    }


    public void onChecked(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            getData().setIsDefault(true);
        } else {
            getData().setIsDefault(false);
        }
        //调用删除接口
//        OrderInfoApiUtils.setArea(getData(), jsonObject -> {
//            RxBus.getDefault().post(new EditAddressEvent(true, adapter.findPos(this), getData()));
//        }, throwable -> {
//
//        });
//        ECAlertDialog dialog = ECAlertDialog.buildAlert(buttonView.getContext(), isChecked ? "确认设置为默认地址吗？" : "确定取消默认地址吗？", "取消", "确定", (dialog1, which) -> {
//            buttonView.setChecked(false);
//        }, (dialog1, which) -> {
//
//
//        });
//        dialog.show();
    }

    /**
     * 编辑地址
     *
     * @param view
     */
    public void onEditClick(View view) {
        ARouter.getInstance()
                .build(ARouterPath.PATH_ADD_NEW_ADDRESS)
                .withObject("data", getData())
                .withInt("index", adapter.findPos(this))
                .navigation();

    }

    /**
     * 删除地址
     */
    public void onDeleteClick(View view) {
        ECAlertDialog dialog = ECAlertDialog.buildAlert(view.getContext(), "确认删除当前地址？", (dialog1, which) -> {
            //调用删除接口
            OrderInfoApiUtils.deleteCityLisItem(getData().getId(), jsonObject -> {
                adapter.removeItem(this);
//                adapter.notifyItemRemoved(adapter.findPos(this));
//                adapter.notifyDataSetChanged();
            }, throwable -> {
                Log.d(TAG, "onDeleteClick: throwable == " + throwable.toString());
            });

        });
        dialog.show();
    }

}
